
#include "prog_trigger.h"

/**
 * can-ctrl
 * Program: prog_trigger
 */

uint32_t prog_trigger_lastcheck = 0;
int16_t prog_trigger_temperature = 0;

int8_t prog_trigger_init(void)
{
	register_slot(prog_trigger_buttonpressed, btn_event);
	register_slot(prog_trigger_potchange,pot_event);
	lcd_clear_command_line();
	prog_trigger_update_text(can_message_get_temperature());
	return 0;
}
int8_t prog_trigger_uninit(void)
{
	unregister_slot(prog_trigger_buttonpressed, btn_event);
	unregister_slot(prog_trigger_potchange,pot_event);
	lcd_clear_command_line();
	return 0;
}
int8_t prog_trigger_run(void)
{
	
	if(time() - prog_trigger_lastcheck > 100)
	{
		int16_t cur_temp = can_message_get_temperature();
		if(prog_trigger_temperature != cur_temp)
		{
			prog_trigger_update_text(cur_temp);
			can_message_servo_position(1, cur_temp);
			sound(700, 500);
			_delay_ms(500);
			sound(6000, 1000);
		}
		prog_trigger_temperature =  cur_temp;
		
		
		prog_trigger_lastcheck = time();
	}
	return 1;
}
void prog_trigger_update_text(int16_t cur_temp)
{
	usart_write("Trigger event: Temperature change to ");
	char txt[20];
	sprintf(txt, " %iC", cur_temp);
	usart_writeln(txt);
	lcd_write(6, "");
	lcd_write_P(7, PSTR("TRG TEMP "));
	sprintf(txt, " %i@C", cur_temp);
	lcd_write(7, txt);
}
void prog_trigger_buttonpressed(Event *ev)
{
	Button *in_data = (Button *) ev->data;
	
}

void prog_trigger_potchange(Event *ev)
{
	Potentiometer *pot = (Potentiometer *) ev->data;
	uint8_t *value = (uint8_t *) (ev->data +1);
	
//  	if(*pot == pota)
// 		sound(*value * 20, 1000);
}